<!--
 The BSD 3-Clause License

 Copyright 2022 - DATATRONiQ GmbH (https://datatroniq.com)
 Copyright (c) 2018-2022 Klaus Landsdorf (http://node-red.plus/)
 Copyright 2015,2016 - Mika Karaila, Valmet Automation Inc. (node-red-contrib-opcua)
 All rights reserved.
 node-red-contrib-iiot-opcua
-->

<script type="text/javascript">
  RED.nodes.registerType('OPCUA-IIoT-Read', {
    category: 'IIoT',
    color: '#ABCDEF',
    defaults: {
      attributeId: {value: 0, required: true, validate: RED.validators.number()},
      maxAge: {value: 1, validate: RED.validators.number()},
      depth: {value: 1, validate: RED.validators.number()},
      connector: {type: 'OPCUA-IIoT-Connector', required: true},
      name: {value: ''},
      justValue: {value: true},
      showStatusActivities: {value: false},
      showErrors: {value: false},
      parseStrings: {value: false},
      historyDays: {value: 1}
    },
    inputs: 1,
    outputs: 1,
    align: 'left',
    icon: 'icon.png',
    label: function () {
      return this.name || 'Read'
    },
    labelStyle: function () {
      return this.name ? 'node_label_italic' : ''
    },
    oneditprepare: function () {
      try {
        let depthRow = $("#node-input-depth-row")
        let historyRow = $("#node-input-history-row")

        if (parseInt(this.attributeId) === 99) {
          depthRow.show();
        } else {
          depthRow.hide();
        }

        let attributeIdCheckbox = $("#node-input-attributeId")

        attributeIdCheckbox.change(function () {
          if ($(this).val() === "99") {
            depthRow.show()
          } else {
            depthRow.hide()
          }

          if ($(this).val() === "130") {
            historyRow.show()
          } else {
            historyRow.hide()
          }
        })
      } catch
        (err) {
        this.error(err);
      }
    }
  })
</script>


<script type="text/x-red" data-template-name="OPCUA-IIoT-Read">
    <div class="form-row" style="min-width:640px">
        <label for="node-input-connector"><i class="icon-globe"></i> <span data-i18n="opcua-iiot-contrib.label.connector"></span></label>
        <input type="text" id="node-input-connector">
    </div>
     <div class="form-row">
        <!-- AttributeId via REST -->
        <label for="node-input-attributeId"><i class="fa fa-tasks"></i> <span data-i18n="opcua-iiot-contrib.label.attributeId"></span></label>
        <select type="text" id="node-input-attributeId">
            <option value="0">All</option>
            <option value="1">Node-ID</option>
            <option value="2">Node-Class</option>
            <option value="3">Browse-Name</option>
            <option value="4">Display-Name</option>
            <option value="13">Value</option>
            <option value="130">History</option>
        </select>
    </div>
    <div id="node-input-history-row">
       <div class="form-row">
          <label for="node-input-historyDays"><i class="icon-calendar"></i> <span data-i18n="opcua-iiot-contrib.label.historyDays"></span></label>
          <input type="text" id="node-input-historyDays" placeholder="1">
       </div>
    </div>
    <div class="form-row">
        <label for="node-input-maxAge"><i class="fa fa-clock-o"></i> <span data-i18n="opcua-iiot-contrib.label.maxAge"></span></label>
        <input type="text" id="node-input-maxAge" placeholder="1">
    </div>
    <div class="form-row" id="node-input-depth-row">
        <label for="node-input-depth"><i class="icon-random"></i> <span data-i18n="opcua-iiot-contrib.label.depth"></span></label>
        <input type="text" id="node-input-depth" placeholder="1">
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <hr>
    <div class="form-row">
        <label style="min-width:160px" for="node-input-parseStrings"><i class="fa fa-th"></i>
        <span data-i18n="opcua-iiot-contrib.label.parseStrings"></span></label>
        <input type="checkbox" id="node-input-parseStrings" style="max-width:30px">
    </div>
    <div class="form-row">
        <label style="min-width:160px" for="node-input-justValue"><i class="fa fa-simplybuilt"></i>
        <span data-i18n="opcua-iiot-contrib.label.justValue"></span></label>
        <input type="checkbox" id="node-input-justValue" style="max-width:30px">
    </div>
    <hr>
    <div class="form-row">
        <label style="min-width:160px" for="node-input-showStatusActivities"><i class="fa fa-bolt"></i>
        <span data-i18n="opcua-iiot-contrib.label.showActivities"></span></label>
        <input type="checkbox" id="node-input-showStatusActivities" style="max-width:30px">
    </div>
    <div class="form-row">
        <label style="min-width:160px" for="node-input-showErrors"><i class="fa fa-exclamation-circle"></i>
        <span data-i18n="opcua-iiot-contrib.label.showErrors"></span></label>
        <input type="checkbox" id="node-input-showErrors" style="max-width:30px">
    </div>

</script>

<script type="text/x-red" data-help-name="OPCUA-IIoT-Read">
    <h2>OPC UA IIoT Read</h2>

    <p>The Read node is for polling data from the OPC UA server.
    It handles a single data request and multiple data requests.
    The multiple Node-Id inject will request and produce an array of address space data.
    An individual Node-Id inject reads just a value and sends it via payload.</p>

    <h3>Input read</h3>
    <div>
        <ul>
            <li>nodesToRead or addressSpaceItems (Array of Objects with property nodeId)
            <li>payload (optional)
                <ul>
                    <li>maxAge (number)
                    <li>historyStart (date)
                    <li>historyEnd (date)
                </ul>
        </ul>
    </div>

    <p>OPC UA results containing nodesToRead and Injects sending addressSpaceItems.</p>

    <h3>Output</h3>

    <p>Please use the Result Filter node to handle the output of the Read node.</p>

    <div>
        <p>Results of request in message:</p>
        <ul>
            <li>payload (Array/Object or String if not to parse)
            <ul>
                <li>results
                <li>nodesToRead
            </ul>
            <li>topic
            <li>nodetype
            <li>readtype
            <li>attributeId
            <li>resultsConverted (Array/Object of results or null if not to parse)
        </ul>
    </div>

    <strong>
        <span data-i18n="opcua-iiot-contrib.label.maxAge"></span>
    </strong>
    <p>
    Maximum age of the value to be read in milliseconds. (see
    <a href="http://node-opcua.github.io/api_doc/classes/ReadRequest.html" target="_blank">node-opcua API</a>)
    </p>

    <strong>
        <span data-i18n="opcua-iiot-contrib.label.depth"></span>
    </strong>
    <p>The depth to read for example components. (see node-opcua API)</p>

    <strong>Name</strong>
    <p>Name in the flow of Node-RED.</p>

    <strong>
        <span data-i18n="opcua-iiot-contrib.label.justValue"></span>
    </strong>
    <p>With that option, the node does not send an object on out two for less CPU load.</p>

    <p>Set showErrors to get errors from node-opcua on browse.</p>
</script>
